package com.wits.qso.service;

import java.io.File;
import java.util.UUID;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.wits.platform.common.constants.WebConstants;
import com.wits.platform.common.utils.DateUtil;
import com.wits.platform.common.utils.FileUtil;
import com.wits.platform.common.utils.StringUtil;
import com.wits.platform.service.BaseService;
import com.wits.qso.common.enumerate.BusinessType;
import com.wits.qso.common.enumerate.HelpQueStatus;
import com.wits.qso.dao.AttachmentDao;
import com.wits.qso.dao.HelpQueDao;
import com.wits.qso.entity.Attachment;
import com.wits.qso.entity.HelpQue;

@Component
public class HelpQueService extends BaseService<HelpQue, String>{

	private HelpQueDao helpQueDao;
	
	@Resource
	private AttachmentDao attachmentDao;

	@Resource
	public void setHelpQueDao(HelpQueDao helpQueDao) {
		this.baseDao=helpQueDao;
		this.helpQueDao = helpQueDao;
	}

	/**
	 * 
	 * 批量更新问题状态
	 * 
	 * @param idArr 
	 * @param hpqStatus
	 * @throws Exception
	 */
	public void updateHpqStatus(String[] idArr,HelpQueStatus status) throws Exception{
		for(String id : idArr){
			helpQueDao.updateField(HelpQue.class, id,"hpqStatus", status.getValue());
		}
	}


	public void save(HelpQue entity, MultipartFile file,String basePath) throws Exception{
		helpQueDao.save(entity);
		String businessId=entity.getHpqId();
		String oriName="";
		String extName="";
        if (file!=null && !file.isEmpty()) {  
        	
        	oriName=file.getOriginalFilename();
        	extName=StringUtil.getExtName(oriName,'.');
        	//先保存到数据库,然后再上传文件
    		Attachment att = new Attachment();
    		String id=UUID.randomUUID().toString();
    		att.setId(id);
    		att.setBusinessId(businessId);
    		att.setBusinessType(BusinessType.HELP_QUE.getValue());
    		att.setDateCre(DateUtil.getNow());
    		att.setFileSize(file.getSize());
    		att.setOriginalName(oriName);
    		att.setExtName(extName);
    		
    		String newName=id+"."+extName;
    		String filePath=WebConstants.UPLOAD_DIR+File.separator+BusinessType.HELP_QUE.getValue();
    		att.setFilePath(filePath+File.separator+newName);
        	attachmentDao.save(att); 
        	FileUtil.upload(file,basePath+File.separator+filePath,newName);
        } 

	}
	


	
}
